package it.rainbowbreeze.polygen.ui;

import it.rainbowbreeze.polygen.R;
import it.rainbowbreeze.polygen.common.AppEnv;
import it.rainbowbreeze.polygen.common.LogFacility;
import it.rainbowbreeze.polygen.common.ResultOperation;
import it.rainbowbreeze.polygen.data.webservice.WebserviceManager;
import it.rainbowbreeze.polygen.logic.MessageParser;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SentenceViewerFragment extends Fragment {
    private static final String LOG_HASH = SentenceViewerFragment.class.getSimpleName();
    
    private TextView mTxtPhrase;
    private MessageParser mMessageParser;
    private LogFacility mLogFacility;
    private WebserviceManager mWebserviceManager;
    private RefreshNotification mRefreshNotification;
    private boolean mIsLoading;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frasentenceviewer, container, false);
        
        AppEnv appEnv = AppEnv.i(getActivity().getApplicationContext());
        mWebserviceManager = appEnv.getWebserviceManager();
        mMessageParser = appEnv.getMessageParser();
        mLogFacility = appEnv.getLogFacility();
        mTxtPhrase = (TextView) v.findViewById(R.id.sentenceviewer_txtPhrase);
        
        return v;
    }
    
    public void updateText(String sectionUrl) {
        mLogFacility.v(LOG_HASH, "Loading url " + sectionUrl);
        LoadMessageTask task = new LoadMessageTask();
        task.execute(sectionUrl);
    }
    
    public String getText() {
        return mTxtPhrase.getText().toString();
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (null != activity && activity instanceof RefreshNotification) {
            mRefreshNotification = (RefreshNotification) activity;
            if (mIsLoading) mRefreshNotification.startRefresh();
        } else {
            mRefreshNotification = null;
        }
    }
    
    
    private class LoadMessageTask extends AsyncTask<String, Void, ResultOperation<String>> {
        @Override
        protected void onPreExecute() {
            mIsLoading = true;
            if (null != mRefreshNotification) mRefreshNotification.startRefresh();
            mTxtPhrase.setText(R.string.common_msgRefreshing);
        }
        
        @Override
        protected ResultOperation<String> doInBackground(String... params) {
            String url = params[0];
            ResultOperation<String> message = mWebserviceManager.getMessageFromSection(url);
            return message;
        }
        
        @Override
        protected void onPostExecute(ResultOperation<String> result) {
            mIsLoading = false;
            if (null != mRefreshNotification) mRefreshNotification.stopRefresh();
            String rawMessage = result.getResult();
            String messageParsed = mMessageParser.parse(rawMessage);
            if (TextUtils.isEmpty(messageParsed)) {
                mTxtPhrase.setText(R.string.common_msgServerError);
            } else {
                mTxtPhrase.setText(messageParsed);
            }
        }
    }
}
